import { Component, OnInit, Input } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { environment } from '../../../environments/environment';
import { DataService } from '../../services/data.service';
import { HeaderService } from './header.service';


@Component({
	selector: 'app-header',
	templateUrl: './header.component.html',
	styleUrls: ['./header.component.scss'],
	providers: [HeaderService]
})
export class AppHeaderComponent implements OnInit {

	@Input() public layoutMode: String = "";

	@Input() public links: Array<any> = [];

	@Input() public currentUrl: String = "";

	@Input() public nav;

	// 登录用户名
	public loginUserName: any;

	// cookies test
	token: any;
	username: any;



	constructor(
		public router: Router,
		public headerService: HeaderService,
		public dataService: DataService
	) {
	}

	ngOnInit() {
		this.login();
	}
	// 注销登录
	logOut() {
		if (environment.isSSO) {
			window.location.href = 'https://sso.efunds.com.cn/cas/logout?service=https://sso.efunds.com.cn/cas/login'
			sessionStorage.removeItem('menu');
		} else {
			sessionStorage.removeItem('menu');
			this.dataService.hasLoginInfo = false;
		}

	}

	// 获取cookie
	getCookie(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i].trim();
			if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
		}
		return "";
	}

	// 登录控制
	login() {
		if (environment.isSSO) {
			console.log('开启SSO')
			document.cookie = "token=" + "admin";
			document.cookie = "username=" + "admin";
			
			this.username = this.getCookie('username')
			this.token = this.getCookie('token')
			this.token= this.token.replace(/\"/g, "");
			console.log(this.token);
			sessionStorage.setItem('token', this.token);
			sessionStorage.setItem('username', this.username);
			this.loginUserName = this.username
			// console.log(this.getCookie('username'));
			// console.log(this.getCookie('token'));
			this.headerService.getMenuTree().subscribe((data) => {
				if (data) {
					console.log(data);
					if (data.children.length == 0) {
						sessionStorage.setItem('blankPage', '1');
						// window.location.href = '/#/pages/info';
					} else {
						sessionStorage.setItem('blankPage', '0');
						sessionStorage.setItem('currentTitle', "首页");
						sessionStorage.removeItem('menu');
						let menu = JSON.stringify(data.children)
						sessionStorage.setItem('menu', menu);
						// window.location.href = '/#/pages/dashboard/home';
					}
					this.dataService.initTree.emit('load');
				}
			})
		}

		else {
			console.log('未开启SSO')
			if(!this.dataService.hasLoginInfo){
				window.location.href = '/#/login';
			}
			this.loginUserName = sessionStorage.getItem('token');
			
			// this.token = sessionStorage.getItem('test');
			// sessionStorage.setItem('token', this.token);
			// console.log
			// sessionStorage.setItem('username', this.username);
			// this.headerService.getMenuTree().subscribe((data) => {
			// 	if (data) {
			// 		console.log(data);
			// 		if (data.children.length == 0) {
			// 			sessionStorage.setItem('blankPage', '1');
			// 			window.location.href = '/#/pages/info';
			// 		} else {
			// 			sessionStorage.setItem('blankPage', '0');
			// 			sessionStorage.setItem('currentTitle', "首页");
			// 			sessionStorage.removeItem('menu');
			// 			let menu = JSON.stringify(data.children)
			// 			sessionStorage.setItem('menu', menu);
			// 			window.location.href = '/#/pages/dashboard/home';
			// 		}
					
			// 	}
			// })
		}
		this.dataService.initTree.emit('load');
	}

}
